//
//  WDProductCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/5.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDProductCell.h"
#import "WDProduct.h"

@interface WDProductCell ()
@property (weak, nonatomic) IBOutlet UIImageView *logoView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *collectView;

@property (weak, nonatomic) IBOutlet UILabel *applyNumLabel;
/** 首付比例或者首付金额 */
@property (weak, nonatomic) IBOutlet UILabel *downType;

@property (weak, nonatomic) IBOutlet UILabel *carTypeLabel;
//首付比例
@property (weak, nonatomic) IBOutlet UILabel *downPayLabel;
//年利率
@property (weak, nonatomic) IBOutlet UILabel *interestLabel;
//通过率
@property (weak, nonatomic) IBOutlet UILabel *passLabel;
//审批速度
@property (weak, nonatomic) IBOutlet UILabel *speedLabel;


@end

@implementation WDProductCell

+ (instancetype)productCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"productCell";
    
    WDProductCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setProduct:(WDProduct *)product
{
    _product = product;
    
    [self.logoView sd_setImageWithURL:[NSURL URLWithString:product.parent_logo]];
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", product.parent_name, product.name];
    
    if ([product.favorite intValue] == 1)
    {
        self.collectView.hidden = NO;
    } else {
        
        self.collectView.hidden = YES;
    }
    
    self.applyNumLabel.text = [NSString stringWithFormat:@"%zd", product.apply_count];
    
    if ([product.car_type intValue] == 0) {
        self.carTypeLabel.text = @"新车";
    } else {
        self.carTypeLabel.text = @"二手车";
    }
    
    if ([product.downpayment_type intValue] == 1) {//首付比例
        
        if ([product.downpayment_rate rangeOfString:@","].location == NSNotFound) {
            self.downPayLabel.text = [NSString stringWithFormat:@"%@%%", product.downpayment_rate];
        } else {
            NSArray *rateArr = [product.downpayment_rate componentsSeparatedByString:@","];
            self.downPayLabel.text = [NSString stringWithFormat:@"%@%% - %@%%", rateArr.firstObject, rateArr.lastObject];
        }
    } else {//首付金额
        self.downType.text = @"首付金额";
        self.downPayLabel.text = [NSString stringWithFormat:@"%@万", product.downpayment_rate];
    }
    
    self.interestLabel.text = [NSString stringWithFormat:@"%.2f%%", [product.interest_rate floatValue]];
    
    self.passLabel.text = [NSString stringWithFormat:@"%.2f%%", product.pass_rate];
    
    if (product.approve_speed_value == nil) {
        self.speedLabel.text = @"-";
    } else {
        self.speedLabel.text = [NSString stringWithFormat:@"%@", product.approve_speed_value];
    }
    
    [self layoutIfNeeded];
    
    product.sessionH = CGRectGetMaxY(self.downPayLabel.frame) + 15;
    
}


@end
